package backend.service.worklog;

import backend.model.admin.Employee;
import backend.model.admin.Team;
import backend.model.worklog.WorkLogMismatch;
import backend.service.base.DBModelServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class WorkLogMismatchServiceImpl extends DBModelServiceImpl<WorkLogMismatch> implements WorkLogMismatchService {

    @Override
    public Class<WorkLogMismatch> getEntityClass() {
        return WorkLogMismatch.class;
    }

    @Override
    public WorkLogMismatch loadWorkLogMismatchForDateAndEmployee(Employee employee, Date date) {
        List<WorkLogMismatch> result = dbModelDao.findByNamedQueryAndParams("loadWorkLogMismatchForDateAndEmployee",
                new String[]{"employeeId", "date"}, new Object[]{employee.getId(), DateUtils.truncate(date, Calendar.DATE)});
        if (result.size() == 1) {
            return result.get(0);
        }
       return null;
    }

    @Override
    public List<WorkLogMismatch> loadWorkLogMismatchForTimeInterval(Date startDate, Date endDate) {
        List<WorkLogMismatch> result = dbModelDao.findByNamedQueryAndParams("loadWorkLogMismatchForTimeInterval",
                new String[]{"startDate", "endDate"}, new Object[]{
                DateUtils.truncate(startDate, Calendar.DATE), DateUtils.truncate(endDate, Calendar.DATE)});
       return result;
    }

    @Override
    public List<WorkLogMismatch> loadWorkLogMismatchForTimeIntervalAndEmployee(Employee employee, Date startDate, Date endDate) {
        List<WorkLogMismatch> result = dbModelDao.findByNamedQueryAndParams("loadWorkLogMismatchForTimeIntervalAndEmployee",
                new String[]{"employeeId", "startDate", "endDate"}, new Object[]{employee.getId(),
                DateUtils.truncate(startDate, Calendar.DATE), DateUtils.truncate(endDate, Calendar.DATE)});
       return result;
    }

    @Override
    public List<WorkLogMismatch> loadWorkLogMismatchForTimeIntervalAndTeam(Team team, Date startDate, Date endDate) {
        List<WorkLogMismatch> result = dbModelDao.findByNamedQueryAndParams("loadWorkLogMismatchForTimeIntervalAndTeam",
                new String[]{"teamId", "startDate", "endDate"}, new Object[]{team.getId(),
                DateUtils.truncate(startDate, Calendar.DATE), DateUtils.truncate(endDate, Calendar.DATE)});
        return result;
    }

    public void deleteWorkLogMismatchesForTimeIntervalAndEmployee(Employee employee, Date startDate, Date endDate) {
        dbModelDao.bulkDelete(loadWorkLogMismatchForTimeIntervalAndEmployee(employee,startDate, endDate));
    }
}
